package org.csu.mypetstore.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.aspect.LogAnnotation;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Account;
import org.csu.mypetstore.domin.Cart;
import org.csu.mypetstore.domin.Inventory;
import org.csu.mypetstore.domin.Order;
import org.csu.mypetstore.persistence.InventoryMapper;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.csu.mypetstore.controller.AccountController.isLogin;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private InventoryMapper inventoryMapper;
    //新增订单
    @LogAnnotation(module="orders",operation = "新增订单")
    @PostMapping("")
    CommonResponse<Order> addOrder(@RequestBody Order order,HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        order.setUsername(commonResponse.getData().getUserid());
        UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemid",order.getItemid());
        String sql = String.format("%s=%s-%d","qty","qty",order.getNum());
        updateWrapper.setSql(sql);
        inventoryMapper.update(null,updateWrapper);
        cartService.DeleteCart(order.getItemid(),commonResponse.getData().getUserid());
        return orderService.addOrder(order);
    }

    //查看我的订单
    @LogAnnotation(module="orders",operation = "查看订单")
    @GetMapping("")
    CommonResponse<List<Order>> getOrderListByUsername(HttpSession session) {
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        return orderService.getOrderListByUsername(commonResponse.getData().getUserid());

    }
    //通过id删除订单
    @LogAnnotation(module="orders",operation = "通过id删除订单")
    @DeleteMapping("/{id}")
    CommonResponse<String> deleteOrderById(@PathVariable int id,HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        return orderService.deleteOrderById(id);
    }
    @GetMapping("browse/{userid}")
    public CommonResponse<List<Order>> browseCarts(@PathVariable String userid){
        List<Order> originList =  orderService.getOrderListByUsername(userid).getData();
        int length = originList.size();
        if(length == 0){
            return CommonResponse.createForError("您还没有订单信息!");
        }
        if(length <=3){
            return CommonResponse.createForSuccess("查找成功",originList);
        }else {
            return CommonResponse.createForSuccess("查找成功",originList.subList(0,3));
        }


    }

}
